Exceptions are nothing but some anomalous conditions that occur during the execution of the program. Exceptions are the conditions or typically an event which may interrupt the normal flow of the program's instructions.
Java Exceptions Tutorials With Examples
- Exceptions in Java
Exceptions are nothing but some anomalous conditions that occur during the execution of the program. Exceptions are the conditions or typically an event which may interrupt the normal flow of the program's instructions.
- Exception
Classes
The hierarchy of exception classes commence from Throwable class which is the base class in java.lang. This class can be instantiated and thrown by the program. The Throwable class is further divided into two subclasses :-
- Catching
and Handling Exceptions
The three exception handler components are used to catch and handle the exceptions. These are try, catch and finally clause. The mechanism to catch an exception in Java is to use try and catch block. - How to
Throw Exceptions
Before catching an exception it is must to be thrown first. This means that there should be a code somewhere in the program that could catch the exception. - Handling Multiple Catch
Clauses
In java when we handle the exceptions then we can have multiple catch blocks for a particular try block to handle many different kind of exceptions that may be generated while running the program. - Nested Try-Catch Blocks
In Java we can have nested try and catch blocks. It means that, a try statement can be inside the block of another try.
- Catching
Normal Exceptions
The exceptions that are generated by methods are referred to as normal exceptions. We have already learned that to catch an exception we use try and catch block. - Making Custom (User
Defined)
Exceptions
If you encounter a situation where none of those exception describe your exception accurately or if you can't find the appropriate exception in the Java API, you can code a class that defines an exception that is more appropriate - What
are Chained Exceptions
Chained exceptions are the exceptions which occur one after another i.e. most of the time to response to an exception is given by an application by throwing another exception. - How to Print a Stack Trace Message
Instead of this this method you can get more information about the error process if you print a stack trace from the exception using the printStackTrace() method that prints the stack trace to the console